package cn.dglydrpy.study.j2ee.mq.rabbitmq.consumer;

import com.rabbitmq.client.*;

import java.io.IOException;

public class DlxConsumer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri("amqp://admin:147258aaaaa@192.168.80.131:5672/%2f");

        final Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();
        
        // 确保MQ中有该队列，如果没有则创建
//      保险起见，先声明一下，如果RabbitMQ的虚拟主机中有该队列，当然好，如果没有，则创建
//      此处的队列应该和生产者声明的队列属性等一致
        channel.queueDeclare("queue.dlx", true, false, false, null);
        
// basicConsume 方式二：监听消息，一旦有消息推送过来，就调用第一个lambda表达式
        // 第二个参数 true:自动确认，
		channel.basicConsume("queue.dlx",true,(consumerTag, message) -> {
            System.out.println(new String(message.getBody()));
        }, (consumerTag) -> {System.out.println("consumerTag  :  " + consumerTag);});

//        channel.close();
//        connection.close();
    }
}
